package com.cskaoyan.market.service.impl;

import com.cskaoyan.market.db.domain.MarketRegion;
import com.cskaoyan.market.db.domain.MarketRegionExample;
import com.cskaoyan.market.db.domain.SelfMarketRegion;
import com.cskaoyan.market.db.mapper.MarketRegionMapper;
import com.cskaoyan.market.service.MarketRegionService;
import com.cskaoyan.market.util.MyBatisUtil;
import org.apache.ibatis.session.SqlSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
@Service
public class MarketRegionServiceImpl implements MarketRegionService {
    @Autowired
    MarketRegionMapper mapper;
    @Override
    public List<MarketRegion> getRegionList(Integer pid) {
        MarketRegionExample marketRegionExample = new MarketRegionExample();
        marketRegionExample.or().andPidEqualTo(pid);
        List<MarketRegion> marketRegions = mapper.selectByExample(marketRegionExample);
        return marketRegions;
    }
    //递归调用打包List
    public List<SelfMarketRegion> packageMarketRegionList(List<MarketRegion> regionList){
        List<SelfMarketRegion> selfMarketRegions = new ArrayList<>();
        for (MarketRegion marketRegion : regionList) {
            if (marketRegion.getType()==3){
                selfMarketRegions.add(new SelfMarketRegion(marketRegion));
            }else {
                SelfMarketRegion selfMarketRegion = new SelfMarketRegion(marketRegion);
                List<SelfMarketRegion> packagedRegions = packageMarketRegionList(getRegionList(marketRegion.getId()));
                selfMarketRegion.setChildren(packagedRegions);
                selfMarketRegions.add(selfMarketRegion);
            }
        }
        return selfMarketRegions;
    }
}
